package com.sgb.member.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.member.MemberConfig;
import com.sgb.member.R;
import com.sgb.member.databinding.FragmentZhaoCaiBinding;
import com.sgb.member.entity.MemberTenderBean;
import com.sgb.member.ui.adapter.HomeOtherMemberAdapter;
import com.sgb.member.view_model.ZhaoCaiViewModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.customview.XCustomTabLayout;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.MToast;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.xrecyclerview.LoadingMoreFooter;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * 企业服务
 */
public class ZhaoCaiServiceFragment extends AppLazyFragment {

    private FragmentZhaoCaiBinding mBinding;
    private ZhaoCaiViewModel viewModel;
    private HomeOtherMemberAdapter<MemberTenderBean> adapter;//企业服务
    private List<CommonPopEntity> allTabs = new ArrayList<>();
    private String tab1 = "";
    private boolean noMoreData = false;
    private boolean isLoadMore = true;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_zhao_cai, container, false);
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        viewModel = new ViewModelProvider(this).get(ZhaoCaiViewModel.class);
        viewModel.getTenderNewSubTypeList();
        initRecyelerView();
        receiverLiveData();
    }

    public void refreshTabData() {
        viewModel.getTenderNewSubTypeList();
    }

    @Override
    protected void onLazyLoad() {
        loadData();
    }

    private void initRecyelerView() {
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        LoadingMoreFooter footer = (LoadingMoreFooter) mBinding.recyclerView.getFootView();
        footer.setNoMoreHint("已经到底啦~");
        mBinding.recyclerView.setPullRefreshEnabled(false);
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isLoadMore = false;
                loadData();
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                loadData();
            }
        });
    }

    public void loadData() {
        if (noMoreData && isLoadMore) {
            closeRecyclerRefreshing();
            return;
        }
        initRecyclerAdapter();
        viewModel.findTenderingHomeEsPage(isLoadMore, tab1);
    }

    public void closeRecyclerRefreshing() {
        mBinding.recyclerView.refreshComplete();
        mBinding.recyclerView.loadMoreComplete();
        Fragment parentFragment = getParentFragment();
        if (parentFragment instanceof HomeMemberFragment) {
            ((HomeMemberFragment) parentFragment).closeRecyclerRefreshing();
        }
    }

    private void initRecyclerAdapter() {
        if (mBinding.recyclerView.getItemDecorationCount() > 0) {
            mBinding.recyclerView.removeItemDecorationAt(0);
        }
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mBinding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getResources().getDimension(R.dimen.dp1)));
        adapter = new HomeOtherMemberAdapter<MemberTenderBean>(getActivity(), R.layout.item_other_tender, null);
        adapter.setOnItemClick(new AdapterOnItemClick<MemberTenderBean>() {
            @Override
            public void onItemClick(MemberTenderBean bean, int position) {
                viewModel.tenderItemClick(bean);
            }
        });
        adapter.onIChildClick(memberTenderBean -> viewModel.tenderItemClick(memberTenderBean));
        mBinding.recyclerView.setAdapter(adapter);
    }


    private void receiverLiveData() {
        viewModel.isLoadDialogLiveData.observe(this, aBoolean -> showOrHideDialog(aBoolean));
        viewModel.allTabDataLiveData.observe(this, commonPopEntities -> {
            allTabs.clear();
            allTabs.addAll(commonPopEntities);
            setTab1();
        });
        viewModel.isEmptyLiveData.observe(this, aBoolean -> {
            mBinding.recyclerView.setVisibility(View.GONE);
            mBinding.inEmpty.setVisibility(View.VISIBLE);
        });
        viewModel.listLiveData.observe(this, beanList -> setListData(beanList));
        viewModel.webUrlLiveData.observe(this, url -> MemberConfig.getInstance().startWebViewX5(requireContext(), url));
        viewModel. msgLiveData.observe(this, data -> MToast.showToast(requireContext(), data));
        viewModel.startPickNoticeDetailLiveData.observe(this, no -> MemberConfig.getInstance().startPickNoticeDetail(requireContext(), no));
        viewModel.startTenderDetailsLiveData.observe(this, id -> MemberConfig.getInstance().startTenderDetails(requireContext(),id, 0));
    }

    private void setListData(List<MemberTenderBean> beanList) {
        closeRecyclerRefreshing();
        mBinding.recyclerView.setVisibility(View.VISIBLE);
        mBinding.recyclerView.setLoadingMoreEnabled(!noMoreData);
        if (beanList.size() < 10) {
            noMoreData = true;
        } else {
            noMoreData = false;
        }
        if (isLoadMore) {
            if (beanList != null && beanList.size() > 0) {
                adapter.addDatas(beanList);
            }
        } else {
            if (beanList != null && beanList.size() > 0) {
                mBinding.inEmpty.setVisibility(View.GONE);
                adapter.setDatas(beanList);
            } else {
                adapter.clearDatas();
                mBinding.inEmpty.setVisibility(View.VISIBLE);
            }
        }
    }


    public void setTab1() {
        mBinding.tabLayout.removeAllTabs();
        if (allTabs != null && allTabs.size() > 0) {
            mBinding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
            mBinding.tabLayout.setVisibility(View.VISIBLE);
            for (int i = 0; i < allTabs.size(); i++) {
                mBinding.tabLayout.addTab(mBinding.tabLayout.newTab().setText(allTabs.get(i).getTypeName()));
            }
            mBinding.tabLayout.setNormalColor(R.color.color_title_small);
            mBinding.tabLayout.addOnTabSelectedListener(allTabs, new XCustomTabLayout.SimpleSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    CommonPopEntity entity = allTabs.get(tab.getPosition());
                    tab1 = entity.getNo();
                    isLoadMore = false;
                    loadData();
                }
            });
        }else {
            mBinding.tabLayout.setVisibility(View.GONE);
        }
    }

    private void showOrHideDialog(boolean aBoolean) {
        if (aBoolean) {
            DialogHelper.showProgressDialog(requireContext(), "数据获取中...");
        } else {
            DialogHelper.dismissProgressDialog();

        }
    }
}
